<?php
/**
 * someObject.class.php
 *
 * Template for creating database query objects
 *
 * @author Klaus
 * @package defaultPackage
 */

class Team {
	
	var $id;

    /**
     * retrieves random record from database table
     *
     */ 
    function getRandom() {
		include_once "DbUtil.class.php";
		$db = new DbUtil();
        $db->connect();
        
 		$sql = 'SELECT id FROM someTable ORDER BY RAND() LIMIT 1';
		$result = $db->query($sql);
		$row = mysql_fetch_assoc($result);
        $this->id = $row[id];

        
        $db->disconnect();
    }
        
    /**
     * inserts values into database table
     *
     * @return integer result from mysql_insert_id()
     */ 
    function insert($value1, $value2) {
		include_once "DbUtil.class.php";
		$db = new DbUtil();
		$db->connect();
        
		$sql= " INSERT INTO someTable"
		." (col1, col2) "
		."VALUES ('".$value1. "', '".$value2. "')";
		mysql_query($sql) or die("<b>Error in File:</b> ".__FILE__."<br /><b>Line:</b> ".__LINE__."<br /><b>SQL-Error:</b> ". mysql_error() ."<br/><b>Query:</b> $sql");
		$result = mysql_insert_id() or die("<b>Error in File:</b> ".__FILE__."<br /><b>Line:</b> ".__LINE__."<br /><b>SQL-Error:</b> ". mysql_error() ."<br/><b>Query:</b> $sql");
		
        $db->disconnect();
        
        return $result;
    }
    
    /**
     * get one database record by ID
     *
     * @param integer $id ID of record to retrieve
     * @return integer result from mysql_insert_id()
     */ 
	function retrieve($id) {
		include_once "DbUtil.class.php";
		$db = new DbUtil();
		$db->connect();
        
		$sql = "SELECT id
				FROM someTable WHERE id ='$id'";
		$rslt = $db->query($sql);
		$row = mysql_fetch_array($rslt);
		$this->id = $row[id];
        
        $db->disconnect();
	}
    
	function retrieveByCC($cc, $lang) {
		include_once "DbUtil.class.php";
		$db = new DbUtil();
		$db->connect();
        
		$sql = "SELECT id, cc, slug, name_".$lang." as name FROM team
				WHERE cc ='$cc'";
		$rslt = $db->query($sql);
		$row = mysql_fetch_array($rslt);
        $this->id = $row[id];
        $this->cc = $row[cc];
        $this->slug = $row[slug];
        $this->name = $row[name];
        
        $db->disconnect();
	}
    
	function retrieveBySlug($slug, $lang) {
		include_once "DbUtil.class.php";
		$db = new DbUtil();
		$db->connect();
        
		$sql = "SELECT id, cc, slug, name_".$lang." as name FROM team
				WHERE slug ='$slug'";
		$rslt = $db->query($sql);
		$row = mysql_fetch_array($rslt);
        $this->id = $row[id];
        $this->cc = $row[cc];
        $this->slug = $row[slug];
        $this->name = $row[name];
        
        $db->disconnect();
	}
	
    /**
     * get all entries from a given table
     *
     * @param string $order_by column to sort by ascending
     * @return integer result from mysql_insert_id()
     */ 
	function retrieveAllByLang($order_by, $lang) {
		include_once "DbUtil.class.php";
		$db = new DbUtil();
		$db->connect();
        
		$all = array(); 
		$i=0; 
		$sql = "SELECT id, cc, slug, name_".$lang." as name FROM team WHERE cc != ''";
		if ($order_by) {
			$sql .= " ORDER BY $order_by ASC";
		}
		$result = $db->query($sql);
		while($row = mysql_fetch_assoc($result)){
  			$obj = new Team();
  			
			$obj->id = $row[id];
            $obj->cc = $row[cc];
            $obj->slug = $row[slug];
            $obj->name = $row[name];
			
  			$all[$i] =$obj;
   			$i++;
		}
        
        $db->disconnect();
        
		return $all;
	}
    
    /**
     * get all entries from a given table
     *
     * @param string $order_by column to sort by ascending
     * @return integer result from mysql_insert_id()
     */ 
	function retrieveAllByGroup($order_by, $lang, $group_id) {
		include_once "DbUtil.class.php";
		$db = new DbUtil();
		$db->connect();
        
		$all = array(); 
		$i=0; 
		$sql = "SELECT id, cc, slug, name_".$lang." as name FROM team
            WHERE cc != '' AND
            group_id = $group_id";
		if ($order_by) {
			$sql .= " ORDER BY $order_by ASC";
		}
		$result = $db->query($sql);
		while($row = mysql_fetch_assoc($result)){
  			$obj = new Team();
  			
			$obj->id = $row[id];
            $obj->cc = $row[cc];
            $obj->slug = $row[slug];
            $obj->name = $row[name];
			
  			$all[$i] =$obj;
   			$i++;
		}
        
        $db->disconnect();
        
		return $all;
	}
    
    /* update given entry in the database 
     *
     * @return integer result from mysql_query()
     */ 
	function update(){
		include_once "DbUtil.class.php";
		$db = new DbUtil();
		$db->connect();
        
		$sql= " UPDATE someTable SET
			col1= '".$this->value1."'
			WHERE id = '".$this->pid."'";
		$result = mysql_query($sql) or die(mysql_error());
        
        $db->disconnect();
        
		return $result;
	}
	
    /* delete given entry from the database 
     *
     * @param integer $id ID of record to delete
     * @return integer result from mysql_query()
     */ 
	function delete($id) {
		include_once "DbUtil.class.php";
		$db = new DbUtil();
		$db->connect();
        
		// delete from sight table
		$sql= "DELETE FROM busstation"
		." WHERE point_id = '$id'" ;
		$result = mysql_query($sql) or die("<b>Error in File:</b> ".__FILE__."<br /><b>Line:</b> ".__LINE__."<br /><b>SQL-Error:</b> ". mysql_error() ."<br/><b>Query:</b> $sql");
        
        $db->disconnect();
		return $result;
	}
    
    /* find record in database based on given criteria 
     *
     * @param array $criteria array of key-value-pairs for search criteria
     * @param string $order_by column to sort by ascending
     * @param string $operator comparison operator, e.g. "="
     * @return integer result from mysql_query()
     */ 
	function find($criteria, $order_by, $operator){
		$all = array();
		$i = 0;
		$sql= "SELECT id"
		." FROM someTable"
		." WHERE ";
		$j=1;
		$critcount = count($criteria);
		foreach ($criteria as $key => $value){ ;
			$sql .= " $key $operator '$value' " ;
			if ($j<$critcount) $sql .= "AND ";
			$j++;
		}
		if($order_by!=null){
		    $sql .= " ORDER BY " . $order_by;
		}
		$sql .= ";";
		$result = mysql_query($sql) or die("<b>Error in File:</b> ".__FILE__."<br /><b>Line:</b> ".__LINE__."<br /><b>SQL-Error:</b> ". mysql_error() ."<br/><b>Query:</b> $sql");
		while($row = mysql_fetch_assoc($result)){
  			$obj = new someObject();
  			$obj->id = $row['id'];

  			$all[$i] =$obj;
   			$i++;
		}
        
        $db->disconnect();
		return $all;
	} 
}
?>